package cn.exrick.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.exrick.mapper.TbFriendMapper;
import cn.exrick.pojo.TbFriend;
import cn.exrick.service.FriendService;
import cn.exrick.utils.mybatis.Criteria;
import cn.exrick.utils.mybatis.Page;
import cn.exrick.utils.mybatis.PageParam;

@Service
public class FriendServiceImpl extends BaseServiceImpl<TbFriend, TbFriendMapper> implements FriendService {
    private final static Logger log = LoggerFactory.getLogger(FriendServiceImpl.class);

    @Override
    public Logger getLogger() {
        return log;
    }

    public Criteria createCriteria(Map<String, Object> params) {
        Criteria criteria = createCriteria();
        if (params.get("friendId") != null) {
            criteria.andFieldEqualTo("friend_id", String.valueOf((Long) params.get("friendId")));
        }
        if (params.get("memberId") != null) {
            criteria.andFieldEqualTo("member_id", String.valueOf((Long) params.get("memberId")));
        }
        return criteria;
    }

    @Autowired
    TbFriendMapper tbFriendMapper;

    @Override
    public TbFriendMapper getMapper() {
        return tbFriendMapper;
    }

    public TbFriend findByFriendAndMember(long friendId, long memberId) {
        List<TbFriend> list = findListByCriteria(new HashMap<String, Object>() {{
            this.put("friendId", friendId);
            this.put("memberId", memberId);
        }});
        if (list == null || list.size() <= 0) {
            return null;
        } else {
            return list.get(0);
        }
    }

    public List<TbFriend> findByMemberId(long memberId) {
        return findListByCriteria(new HashMap<String, Object>() {{
            this.put("memberId", memberId);
        }});
    }

    public Page<TbFriend> findPageByMemberId(long memberId, PageParam pageParam) throws Exception {
        return findPageByCriteria(new HashMap<String, Object>() {{
            this.put("member_id", memberId);
        }}, pageParam);
    }
}
